Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    focus: false,
    shows: false,
    imgText: [
      '即日起莱州信息网发布信息需实名认证',
      '免费注册网站会员可自己修改/删除信息'
    ],
    imgUrls: [
      'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
      'https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640',
      'https://images.unsplash.com/photo-1551446591-142875a901a1?w=640'
    ],
    interval: 3000,
    duration: 1000,
    pageSize: 10,
    currentPage: 0,
    limit: 10,
    list:[],
    list2:[],
    obj:{},
    objType:{}
  },
  home: function() {
    wx.switchTab({
      url: '../home/home',
    })
  },
  // 房屋租售
  ershou: function() {
    wx.navigateTo({
      url: '../peijian/peijian',
    })
  },
  concant1: function() {
    this.setData({
      show: !this.data.show
    })
  },
  concants: function() {
    this.setData({
      shows: !this.data.shows
    })
  },
  // 首页详情
  homedetails: function(e) {
    var obj = e.currentTarget.dataset
    wx.setStorage({
      key: 'zxwx-xcx-1903',
      data: obj,
    })
    wx.navigateTo({
      url: '../homedetails/homedetails',
    })
  },
  // 页面渲染  
  getDate: function() {
    var db = wx.cloud.database();
    var that = this;
    var skip = (that.data.currentPage * that.data.pageSize)+1;
    db.collection('laizhou').skip(skip).limit(that.data.limit).orderBy('date', 'desc').get({
      success: function(res) {
        if (res.data.length == 0){
          wx.showToast({
            title: '没有更多数据...'
          })
        }else{
          that.setData({
            list: that.data.list.concat(res.data)
          })
          wx.hideLoading();
          wx.hideNavigationBarLoading();
          wx.stopPullDownRefresh()
        }
        if(!that.data.list2.length){
          that.getList2(0)
        }
      }
    })
  },
  getList2(i) {
    var db = wx.cloud.database();
    var that = this;
    var k = i * 20 + 1;
    db.collection('lzType').skip(k).get({
      success: function (res) {
        if (res.data.length == 0) {
          return;
        }
        that.setData({
          list2: that.data.list2.concat(res.data),
        })
        that.getList2(i + 1)
      }
    })
  },
  // 市场
  shichang:function(e){
    var objType = e.currentTarget.dataset.item;
    var db = wx.cloud.database();
    var that = this;
    db.collection('laizhou').where({
      type:objType
    }).get({
      success: function (res) {
       that.setData({
         list:res.data
       })
      }
    })
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '拼命加载中',
    })
    var that = this;
    wx.getStorage({
      key: 'zxwx-xcx-1903',
      success(res) {
      console.log(res)
        that.setData({
          obj: res.data.item
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getDate()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   * 下拉的时候json需要加一个配置
   */
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      currentPage: 0
    })
    wx.showNavigationBarLoading()
    this.getDate()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.setData({
      currentPage: this.data.currentPage + 1
    })
    this.getDate()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})